package com.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.order.mapper.ProductCategoryMapper;
import com.order.pojo.ProductCategory;
import com.order.pojo.ProductInfo;
import com.order.mapper.ProductInfoMapper;
import com.order.service.IProductInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class ProductInfoServiceImpl extends ServiceImpl<ProductInfoMapper, ProductInfo> implements IProductInfoService {

    @Autowired
    ProductInfoMapper productInfoMapper;
    @Autowired
    ProductCategoryMapper productCategoryMapper;

    @Override
    public PageInfo<ProductInfo> getCategoriesByPage(Integer page, Integer size, ProductInfo productInfo) {
        PageHelper.startPage(page, size);
        QueryWrapper<ProductInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(true, "product_name", productInfo.getProductName());
        queryWrapper.eq(true, "product_del", 0);
        List<ProductInfo> categories = productInfoMapper.selectList(queryWrapper);
        for (ProductInfo category : categories) {
            category.setCategoryName(productCategoryMapper.selectById(category.getCategoryType()).getCategoryName());
        }
        return PageInfo.of(categories);
    }
}
